<?php
# ============================================================================
# 版权所有  Chilcoo.com，并保留所有权利。
# 网站地址: http://www.chilcoo.com/erphp
# ----------------------------------------------------------------------------
# 这不是一个自由软件！你不能通过任何方式破解本软件.
# 使用:不允许对程序代码以任何形式任何目的的再发布。
# ============================================================================
# $Author: Chilcoo Inc. *
# $Official website : www.chilcoo.com
# 
# 文件信息:
# File : /ship/default.php
# 分类列表页面

# 载入常用集
# 导入common
require_once (dirname(__FILE__).'/../../include/common.inc.php');
require_once (dirname(__FILE__).'/../../include/_manager/login.inc.php');
# 页面级别
$conf_web_page_level = 0;

class page_index extends page{
	function __construct(){
		global $config;
		$this->database = new db(config::conf_db_host,config::conf_db_user,
			config::conf_db_password,config::conf_db_name);
	}
	function len(){
		global $config;
		$l = 0;
		$sql = "SELECT count(*) AS cnt FROM cs2_ship;";
		$row = $this->database->get_one($sql);
		$l = $row['cnt'];
		
		return $l;
	}
	function listview(){
		$lv = null;
		try{
			$sql = "SELECT shipId,priceJp,priceUs,dates,cs2_ship.adminId,cs2_admin.name
				FROM cs2_ship
				LEFT JOIN cs2_admin ON (cs2_ship.adminId=cs2_admin.adminId)";
			$sql = $sql." ORDER BY dates ASC;";
			$lv = $this->database->select($sql);
		}catch (Exception $e){
			
		}
		return $lv;
	}
	function ajax_callback(){
		global $config;
		# POST 数据
		$token = $_POST['token'];
		# 初始返回用 json
		$json = array("ref"=>"error");
		if($token == config::TOKEN_ADD_SHIP){
			$dates = $_POST['dates'];
			$priceJp = $_POST['priceJp'];
			$priceUs = $_POST['priceUs'];
			$adminId = $_POST['adminId'];
			$sql = "INSERT INTO cs2_ship (dates, `priceJp`, `priceUs`, adminId) 
			VALUES ('".$dates."',".$priceJp.",".$priceUs.",".$adminId.");";
			try {
				$this->database->insert($sql);
				$json = array("ref"=>"OK");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		if($token == config::TOKEN_UP_SHIP){
			$shipId = $_POST['shipId'];
			$dates = $_POST['dates'];
			$priceJp = $_POST['priceJp'];
			$priceUs = $_POST['priceUs'];
			$adminId = $_POST['adminId'];
			$sql = "UPDATE cs2_ship SET 
				dates='".$dates."', 
				`priceJp`=".$priceJp.",
				`priceUs`=".$priceUs.",
				adminId=".$adminId." 
			WHERE shipId=".$shipId.";";
			try {
				$this->database->update($sql);
				$json = array("ref"=>"OK");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		return json_encode($json);
	}
}

$pg = new page_index();
# Session 和 POST 安全访问.
if($_SERVER['REQUEST_METHOD'] != 'POST'){
	$_SESSION[config::conf_session_id_token] = $config->web_page_period;
	$config->smarty->assign('len', $pg->len());
	$config->smarty->assign('listview', $pg->listview());
	$config->smarty->assign('DATENOW',date("Y-m-d"));
	# $config->smarty->assign('len', $pg->len());
	# 加载页面
	$pg->render($config->temp_page_path.config::conf_web_template_extension);
}else{
	print_r($pg->ajax_callback());
}




?>